package com.sky.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Slf4j
@Service
public class SetMealServiceImpl implements SetMealService {
    @Resource
     private  SetMealMapper setMealMapper;
    @Resource
    private SetMealDishMapper setMealDishMapper;
    //增加套餐
    @Override
    @Transactional
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    public void InsertSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setMealMapper.insertSetMeal(setmeal);
        //将DTO中存储的meal-dish数据获取出来
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes.size()!=0&&!Objects.isNull(setmealDishes)){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            log.info("==============插入时传递的setmealId：{}",setmeal.getId());
        }
        setMealDishMapper.InsertSetmealDishes(setmealDishes);
    }
    //分页查询
    @Override
    public PageResult SelectPageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO>  setmeals= setMealMapper.SelectPageQuery(setmealPageQueryDTO);

        return PageResult.builder().total(setmeals.getTotal()).records(setmeals.getResult()).build();
    }
    //修改出售状态
    @Override
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    public void updateStatus(Integer status,Long setMealId) {
        Setmeal setmeal = Setmeal.builder().id(setMealId).status(status).build();
        List<Integer> statusList= setMealMapper.selectByidForStatus(setmeal);
       if (status==1 && statusList.contains(0)) {
               throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        setMealMapper.updateSetmeal(setmeal);
    }
    //更新套餐信息
    @Override
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.updateSetmeal(setmeal);
        Long setmealId = setmealDTO.getId();
        setMealDishMapper.deleteBysetmealId(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!Objects.isNull(setmealDishes)&&setmealDishes.size()!=0){
            setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmeal.getId());});
            setMealDishMapper.InsertSetmealDishes(setmealDishes);
        }

    }
    //删除套餐信息
    @Override
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    public void deleteSetMealById(List<Long> ids) {
        //查询套餐售卖状态
      List<Setmeal> setmealList=  setMealMapper.SelectStatusById(ids);
      setmealList.forEach(new Consumer<Setmeal>() {
          @Override
          public void accept(Setmeal setmeal) {
              if (setmeal.getStatus().equals(1)){
                  throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
              }
          }
      });
      setMealMapper.deleteByid(ids);
      setMealDishMapper.deleteByid(ids);
    }
    //根据setmealId查询
    @Override
    public SetmealVO selectByDishId(Long setmealId) {
        ArrayList<Long> list = new ArrayList<>();
        list.add(setmealId);
        List<Setmeal> setmealList = setMealMapper.SelectStatusById(list);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmealList.get(0), setmealVO);
        List<SetmealDish> setmealDisheList= setMealDishMapper.selectByDishId(setmealId);
        setmealVO.getSetmealDishes().addAll(setmealDisheList);
        return setmealVO;
    }
    /**
     * 基于分类Id查询套餐信息
     *
     * @param categoryId 分类Id
     * @return 指定分类套餐List集合
     */
    @Override
    public List<Setmeal> selectSetmealByCategoryId(Long categoryId) {
        //封装用于查询套餐信息的Setmeal条件
        Setmeal conditionSetmeal = Setmeal.builder().status(1).categoryId(categoryId).build();
        //调用持久层传递保存了查询参数的条件对象进行查询
        List<Setmeal> setmealList = setMealMapper.selectByCondition(conditionSetmeal);
        return setmealList;
    }

    /**
     * 查询套餐中包含的菜品信息
     *
     * @param setmealId 套餐Id
     * @return 套餐中菜品的基本信息
     */
    @Override
    public List<DishItemVO> selectSetmealDishBySetmealId(Long setmealId) {
        //调用套餐菜品关联持久层查询套餐中菜品的基本信息
        List<DishItemVO> dishItemVOList = setMealDishMapper.selectSetmealDishBySetmealId(setmealId);
        return dishItemVOList;
    }
}
